/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * crearMedicamento.java
 *
 * Created on 22/03/2012, 03:02:44 PM
 */
package co.edu.eam.clinica.vista.gui.inventario;

import co.edu.eam.clinica.vista.controladores.inventario.controladorMedicamento;
import co.edu.eam.clinica.vista.gui.common.PanelOncologos;
import co.edu.eam.persitencia.entidades.Laboratorio;
import co.edu.eam.persitencia.entidades.Medicamento;
import co.edu.eam.persitencia.enumeraciones.GrupoTerapeuticoEnum;
import co.edu.eam.persitencia.enumeraciones.PresentacionEnum;
import co.edu.eam.persitencia.enumeraciones.RevisionSistemaEnum;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.management.MBeanAttributeInfo;
import javax.swing.JOptionPane;

/**
 *
 * @author diana
 */
public class crearMedicamento extends PanelOncologos {

    private controladorMedicamento conMedicamento;

    /**
     * Creates new form crearMedicamento
     */
    public crearMedicamento() {
        initComponents();
        setVisible(true);
        conMedicamento = new controladorMedicamento();
        llenarGrupo();
        llenarPresentacion();


    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jLabel2 = new javax.swing.JLabel();
        jTxCodArticulo = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTxNomArticulo = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTxConsentracion = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jRbSiPos = new javax.swing.JRadioButton();
        jLabel6 = new javax.swing.JLabel();
        jRbNoPos = new javax.swing.JRadioButton();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jTxRegInvima = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jTxLote = new javax.swing.JTextField();
        jBGuardar = new javax.swing.JButton();
        jBBuscar = new javax.swing.JButton();
        jBEditar = new javax.swing.JButton();
        jTxGrupTerapeutico = new javax.swing.JComboBox();
        jTxPresentacion = new javax.swing.JComboBox();
        jTxLabo = new javax.swing.JComboBox();

        jLabel2.setText("Codigo:");

        jLabel3.setText("Nombre:");

        jLabel4.setText("Consentracion:");

        jLabel5.setText("Grupo Terapeutico:");

        jRbSiPos.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jRbSiPos.setText("SI");

        jLabel6.setText("NO POS:");

        jRbNoPos.setText("NO");

        jLabel7.setText("Presentacion:");

        jLabel8.setText("Registro Invima:");

        jLabel9.setText("Laboratorio:");

        jLabel10.setText("Lote:");

        jBGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/torresquintero/oncologos/medico/imagenes/imagenes/16x16/guardar-documento-icono-7840-32 - copia.png"))); // NOI18N
        jBGuardar.setText("Guardar");
        jBGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBGuardarActionPerformed(evt);
            }
        });

        jBBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/torresquintero/oncologos/medico/imagenes/imagenes/16x16/Search (2).png"))); // NOI18N
        jBBuscar.setText("Buscar");
        jBBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBBuscarActionPerformed(evt);
            }
        });

        jBEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/torresquintero/oncologos/medico/imagenes/imagenes/16x16/Edit.png"))); // NOI18N
        jBEditar.setText("Editar");
        jBEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBEditarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addGap(76, 76, 76)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTxCodArticulo, javax.swing.GroupLayout.DEFAULT_SIZE, 385, Short.MAX_VALUE)
                            .addComponent(jTxNomArticulo, javax.swing.GroupLayout.DEFAULT_SIZE, 385, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(44, 44, 44)
                        .addComponent(jTxConsentracion, javax.swing.GroupLayout.DEFAULT_SIZE, 385, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8)
                            .addComponent(jLabel9)
                            .addComponent(jLabel10))
                        .addGap(24, 24, 24)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTxRegInvima, javax.swing.GroupLayout.DEFAULT_SIZE, 385, Short.MAX_VALUE)
                            .addComponent(jTxLote, javax.swing.GroupLayout.DEFAULT_SIZE, 385, Short.MAX_VALUE)
                            .addComponent(jTxGrupTerapeutico, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jTxPresentacion, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jRbSiPos)
                                .addGap(38, 38, 38)
                                .addComponent(jRbNoPos)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addComponent(jTxLabo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jBGuardar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jBBuscar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jBEditar)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(64, 64, 64)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTxCodArticulo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTxNomArticulo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jTxConsentracion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jTxGrupTerapeutico, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jRbSiPos)
                        .addComponent(jRbNoPos)))
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jTxPresentacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTxRegInvima, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jTxLabo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(14, 14, 14)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel10)
                    .addComponent(jTxLote, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBGuardar)
                    .addComponent(jBBuscar)
                    .addComponent(jBEditar))
                .addContainerGap(16, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    public void llenarGrupo() {

        RevisionSistemaEnum[] SistemaEnum = RevisionSistemaEnum.values();
        for (int i = 0; SistemaEnum.length < 10; i++) {
            jTxGrupTerapeutico.addItem(SistemaEnum[i]);
        }

    }

    public void llenarPresentacion() {

        RevisionSistemaEnum[] SistemaEnum = RevisionSistemaEnum.values();
        for (int i = 0; SistemaEnum.length < 10; i++) {
            jTxPresentacion.addItem(SistemaEnum[i]);
        }

    }
    private void jBGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBGuardarActionPerformed
        try {
            // TODO add your handling code here:
            long codigo = Long.parseLong(jTxCodArticulo.getText());
            String nombre = jTxNomArticulo.getText();
            String consentracion = jTxConsentracion.getText();
            Enum grupo = (GrupoTerapeuticoEnum) jTxGrupTerapeutico.getSelectedItem();
            Enum presentacion = (PresentacionEnum) jTxPresentacion.getSelectedItem();
            String registro = jTxRegInvima.getText();
            Laboratorio laboratorio = (Laboratorio) jTxLabo.getSelectedItem();
            String lote = jTxLote.getText();
            boolean noPos;
            if (jRbSiPos.isSelected()) {
                noPos = true;

            } else {
                noPos = false;
            }
            boolean n = noPos;


            Medicamento medicamento = new Medicamento(codigo, nombre, consentracion,
                    GrupoTerapeuticoEnum.ALGO, PresentacionEnum.JARABE, registro, laboratorio, noPos);
            conMedicamento.crearMedicamento(medicamento);
        } catch (Exception ex) {
            Logger.getLogger(crearMedicamento.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, ex.getMessage(), "ERROR",
                    JOptionPane.ERROR_MESSAGE);;
        }
    }//GEN-LAST:event_jBGuardarActionPerformed

    private void jBBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBBuscarActionPerformed
        long id = Long.parseLong(jTxCodArticulo.getText());
        try {
            // TODO add your handling code here:
            Medicamento med = conMedicamento.buscarMedicamento(id);
            if (med != null) {
                jTxNomArticulo.setText(med.getNombre());
                jTxConsentracion.setText(med.getConsentracion());
                jTxGrupTerapeutico.setSelectedItem(med.getGrupoTerapeutico());
                jTxPresentacion.setSelectedItem(med.getPresentacion() + "");
                jTxRegInvima.setText(med.getRegistroInvima());
                jTxLabo.setSelectedItem(med.getLaboratorio() + "");


            }
            JOptionPane.showMessageDialog(null, "No existe", "ERROR",
                    JOptionPane.ERROR_MESSAGE);
        } catch (Exception ex) {
            Logger.getLogger(crearMedicamento.class.getName()).log(Level.SEVERE, null, ex);
        }


    }//GEN-LAST:event_jBBuscarActionPerformed

    private void jBEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBEditarActionPerformed

        long codigo = Long.parseLong(jTxCodArticulo.getText());
        String nombre = jTxNomArticulo.getText();
        String consentracion = jTxConsentracion.getText();
        Enum grupo = (GrupoTerapeuticoEnum) jTxGrupTerapeutico.getSelectedItem();
        Enum presentacion = (PresentacionEnum) jTxPresentacion.getSelectedItem();
        String registro = jTxRegInvima.getText();
        Laboratorio laboratorio = (Laboratorio) jTxLabo.getSelectedItem();
        String lote = jTxLote.getText();
        boolean noPos;
        if (jRbSiPos.isSelected()) {
            noPos = true;

        } else {
            noPos = false;
        }
        boolean n = noPos;


        try {
            Medicamento m = new Medicamento(codigo, nombre, consentracion,
                    GrupoTerapeuticoEnum.ALGO, PresentacionEnum.JARABE, registro, laboratorio, noPos);

            if (m != null) {
                conMedicamento.editarMedicamento(m);

            } else {
                JOptionPane.showMessageDialog(this, "MEDICAMENTO NO EXISTE");
            }
        } catch (Exception ex) {
            Logger.getLogger(crearMedicamento.class.getName()).log(Level.SEVERE, null, ex);
        }



    }//GEN-LAST:event_jBEditarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jBBuscar;
    private javax.swing.JButton jBEditar;
    private javax.swing.JButton jBGuardar;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JRadioButton jRbNoPos;
    private javax.swing.JRadioButton jRbSiPos;
    private javax.swing.JTextField jTxCodArticulo;
    private javax.swing.JTextField jTxConsentracion;
    private javax.swing.JComboBox jTxGrupTerapeutico;
    private javax.swing.JComboBox jTxLabo;
    private javax.swing.JTextField jTxLote;
    private javax.swing.JTextField jTxNomArticulo;
    private javax.swing.JComboBox jTxPresentacion;
    private javax.swing.JTextField jTxRegInvima;
    // End of variables declaration//GEN-END:variables
}
